package lib.self.adapter.recycler;

import androidx.annotation.IdRes;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import android.util.SparseArray;
import android.view.View;

import lib.self.adapter.interfaces.IViewHolder;

/**
 * description: Recycler ViewHolder 基本方法
 * author: Steven Xiao
 * date: 2017/5/16
 */
public class RecyclerViewHolderEx extends ViewHolder implements IViewHolder {

    private SparseArray<View> mMap;

    public RecyclerViewHolderEx(View itemView) {
        super(itemView);

        mMap = new SparseArray<>();
    }

    @Override
    public View getConvertView() {
        return itemView;
    }

    @Override
    public View findViewById(@IdRes int id) {
        return itemView.findViewById(id);
    }

    @Override
    public <T extends View> T getView(@IdRes int id) {
        View view = mMap.get(id);
        if (view == null) {
            view = findViewById(id);
            mMap.put(id, view);
        }
        return (T) view;
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        mMap.clear();
        mMap = null;
    }
}
